package ru.msu.cmc.sp.oberon2;
import java.util.*;

import org.antlr.runtime.RecognitionException;

public class Scope {
	LinkedHashMap<String, Symbol> symbols = new LinkedHashMap<String, Symbol>();
	Scope enclosing_scope = null;
	
	public LinkedHashMap<String, Symbol> getSymbols() {
		return symbols;
	}
	public Scope(Scope exs) {
		enclosing_scope = exs;
	}
	
	public Scope getEnclosingScope() {
		return enclosing_scope;
	}
	
	public Symbol define(Symbol sym){
		if (symbols.get(sym.name) != null) {
			return null;
		}
		symbols.put(sym.name, sym);
		sym.scope = this;
		return sym;
	}
	
	public Symbol resolve(String name) {
		Symbol s = symbols.get(name);
        if ( s!=null ) return s;
		if ( enclosing_scope != null ) return enclosing_scope.resolve(name);
		return null;
	}

	public Scope clone() {
		Scope clone = new Scope(enclosing_scope);
		clone.symbols = HashMapCloner.clone(symbols);
		return clone;
	}
}
